class Array
  def shuffle
    sort_by { rand }
  end

  def shuffle!
    self.replace shuffle
  end
end

module Enumerable
  def group_by &block
    groups = Hash.new(){|hash, key| hash[key] = []}
    each {|item|
      retval = block.call(item)
      retval = "Unknown" if retval == "" || retval.nil?
      groups[retval.to_sym] << item
    }
    groups.values
  end

  def decorate &block
    decorated = []
    each_with_index {|item, index|
      decorated << [block.call(item, index), item]
    }
    decorated
  end

  def sort_by_decoration
    sort {|this, that|
      this[0] <=> that[0]
    }
  end

  def undecorate
    map {|item| item[1]}
  end
end
